<?php 
    
	require_once '../funciones.php';

        $soporte=$_SESSION['caweb']['soporte'];
	$page = $_REQUEST['page']; 

	// get how many rows we want to have into the grid - rowNum parameter in the grid 
	$limit = $_REQUEST['rows']; 

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel 
	$sidx = $_REQUEST['sidx']; 

	// sorting order - at first time sortorder 
	$sord = $_REQUEST['sord']; 

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;

        //Validacion Datos de la consulta
        $fechaIni   = ($_REQUEST['fechaini'] != "")?$_REQUEST['fechaini']:'1986-01-01';
        $fechaFin   = ($_REQUEST['fechaFin'] != "")?$_REQUEST['fechaFin']:'2999-01-01';
        $nit        = $_REQUEST['nit'];
        $factura    = $_REQUEST['factura'];
        $sucursal   = $_REQUEST['sucursal'];
        //Se construye la condicion
        $where = ($nit != "")?" and cliente = '$nit'":"";
        $where .= ($factura != "")?" and recaudo = '$factura'":"";
        $where .= ($sucursal != "")?" and sucursal = '$sucursal'":"";



        $con = con_caweb();

//	// connect to the MySQL database server
//	$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
//
//	// select the database
//	mysql_select_db($database) or die("Error connecting to db.");
//
	// calculate the number of rows for the query. We need this for paging the result 

	$result = mysql_query("SELECT COUNT(*) AS count FROM recaudos where fecharecaudo between '$fechaIni' and '$fechaFin' $where ");
	$row = mysql_fetch_array($result,MYSQL_ASSOC); 
	$count = $row['count']; 

	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
//	$SQL = "SELECT * FROM abonosfacturacion WHERE transaccion > 0 GROUP BY transaccion ORDER BY $sidx $sord LIMIT $start , $limit";

	$SQL = "
	SELECT a.recaudo,a.sucursal,a.usuariocreo,a.fechacreo,a.fecharecaudo,a.cliente,b.name AS nombre,a.valorrecaudo,a.clase,a.tipo,a.cheque,a.cuenta,a.banco
	FROM recaudos AS a
	LEFT JOIN  clientes AS b
	ON b.nit = a.cliente where a.fecharecaudo between '$fechaIni' and '$fechaFin'".$where."
	ORDER BY $sidx $sord LIMIT $start , $limit";

	$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";

	$SQL2 = "
	SELECT sum(a.valorrecaudo) as suma FROM recaudos AS a
	LEFT JOIN  clientes AS b
	ON b.nit = a.cliente where a.fecharecaudo between '$fechaIni' and '$fechaFin'".$where."
	ORDER BY $sidx $sord LIMIT $start , $limit";
        $result1 = mysql_query($SQL2) or die("Error SQL 3".mysql_error().$SQL2);
        echo "<row id=''>";
        echo "<cell></cell><cell></cell>";
        echo "<cell>Total</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"suma"),0, ',', '.')."</cell>";
        echo "<cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell>";
        echo "</row>";


	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		echo "<row id='". $row[id]."'>";
		echo "<cell>". $row[recaudo]."</cell>";
		echo "<cell>". $row[fecharecaudo]."</cell>";
		echo "<cell>". $row[sucursal]."</cell>";
		echo "<cell>". number_format($row[valorrecaudo],0, ',', '.')."</cell>";		
		echo "<cell>". $row[cliente]."</cell>";		
		echo "<cell>". utf8_encode(htmlspecialchars($row[nombre]))."</cell>";
		echo "<cell>". $row[tipo]."</cell>";				
		echo "<cell>". $row[clase]."</cell>";
		echo "<cell>". $row[cheque]."</cell>";
		echo "<cell>". $row[banco]."</cell>";
		echo "<cell>". $row[cuenta]."</cell>";
		echo "<cell>". $row[usuariocreo]."</cell>";
		echo "</row>";
	}
	echo "</rows>";
	 
?>
